package com.dyz.leetcode._01_array;

public class _36_isValidSudoku {
    public boolean isValidSudoku(char[][] board) {
        //行
        boolean[][] rowBool = new boolean[9][9];
        for (int row =0; row<9; row++){
            for (int col=0; col<9; col++){
                if(board[row][col]!='.'){
                    int cur = board[row][col]-'0'-1;
                    if(!rowBool[row][cur]){
                        rowBool[row][cur]=true;
                    }else{
                        return false;
                    }
                }
            }
        }

        //列
        boolean[][] colBool = new boolean[9][9];
        for (int col =0; col<9; col++){
            for (int row=0; row<9; row++){
                if(board[row][col]!='.'){
                    int cur = board[row][col]-'0'-1;
                    if(!colBool[cur][col]){
                        colBool[cur][col]=true;
                    }else{
                        return false;
                    }
                }
            }
        }

        //block
        boolean[][] blockBool = new boolean[9][9];
        for (int col =0; col<9; col++){
            for (int row=0; row<9; row++){
                if(board[row][col]!='.'){
                    int cur = board[row][col]-'0'-1;
                    int blockIndex = row/3 +(col/3)*3;
                    if(!blockBool[blockIndex][cur]){
                        blockBool[blockIndex][cur]=true;
                    }else{
                        return false;
                    }
                }
            }
        }

        return true;
    }

    public boolean isValidSudoku1(char[][] board) {
        boolean[][] rowUsed = new boolean[9][9];
        boolean[][] colUsed = new boolean[9][9];
        boolean[][] boxUsed = new boolean[9][9];

        for (int row = 0; row < board.length; row++) {
            for (int col = 0; col < board[0].length; col++) {
                if (board[row][col] != '.') {
                    int num = board[row][col] - '1';

                    if (rowUsed[row][num]) return false;
                    else rowUsed[row][num] = true;

                    if (colUsed[col][num]) return false;
                    else colUsed[col][num] = true;

                    int boxIndex = row / 3 + (col / 3) * 3;
                    if (boxUsed[boxIndex][num]) return false;
                    else boxUsed[boxIndex][num] = true;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        char c = '9';
        System.out.println(c-'0'-1);
    }
}
